package org.loofer.uikit.text;

import java.lang.Character.UnicodeBlock;

public interface LengthConverter {
    public static final LengthConverter CHINESE_CONVERTER = new LengthConverter() {
        public int convert(CharSequence src, int srcStart, int srcEnd) {
            int s;
            int e;
            if (srcStart <= srcEnd) {
                s = srcStart;
            } else {
                s = srcEnd;
            }
            if (srcStart <= srcEnd) {
                e = srcEnd;
            } else {
                e = srcStart;
            }
            int chineseCount = 0;
            int i = s;
            while (i < e && i < src.length()) {
                if (isChinese(src.charAt(i))) {
                    chineseCount++;
                }
                i++;
            }
            return (e - s) + chineseCount;
        }

        public int reverse(CharSequence src, int srcStart, int dstEnd) {
            int s;
            int e;
            if (srcStart <= dstEnd) {
                s = srcStart;
            } else {
                s = dstEnd;
            }
            if (srcStart <= dstEnd) {
                e = dstEnd;
            } else {
                e = srcStart;
            }
            int chineseCount = 0;
            for (int i = s; i < src.length(); i++) {
                if (isChinese(src.charAt(i))) {
                    chineseCount++;
                }
                if (i + chineseCount >= e) {
                    return i - s;
                }
            }
            return 0;
        }

        private boolean isChinese(char c) {
            UnicodeBlock ub = UnicodeBlock.of(c);
            if (ub == UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == UnicodeBlock.GENERAL_PUNCTUATION || ub == UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
                return true;
            }
            return false;
        }
    };
    public static final LengthConverter DEFAULT_CONVERTER = new LengthConverter() {
        public int convert(CharSequence src, int srcStart, int srcEnd) {
            int length = srcEnd - srcStart;
            return length >= 0 ? length : -length;
        }

        public int reverse(CharSequence src, int srcStart, int dstEnd) {
            int length = dstEnd - srcStart;
            return length >= 0 ? length : -length;
        }
    };

    int convert(CharSequence charSequence, int i, int i2);

    int reverse(CharSequence charSequence, int i, int i2);
}
